package sk.vasicek.drinkmanager.service;

import sk.vasicek.drinkmanager.R;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class BackgroundAdapter extends BaseAdapter {
	int mGalleryItemBackground;
	private Context mContext;

	private Integer[] mImageIds = { R.drawable.back_dark, R.drawable.back_steel, R.drawable.back_titan };

	public BackgroundAdapter(Context c) {
		mContext = c;
		TypedArray attr = mContext.obtainStyledAttributes(R.styleable.BackgroundGalery);
		mGalleryItemBackground = attr.getResourceId(R.styleable.BackgroundGalery_android_galleryItemBackground, 0);
		attr.recycle();
	}

	public int getCount() {
		return mImageIds.length;
	}

	public Object getItem(int position) {
		return mImageIds[position];
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView = new ImageView(mContext);

		imageView.setImageResource(mImageIds[position]);
		imageView.setLayoutParams(new Gallery.LayoutParams(200, 125));
		imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		imageView.setBackgroundResource(mGalleryItemBackground);

		return imageView;
	}

	public static Drawable updateBackgroundFromPrefs(SharedPreferences prefs, Activity context) {
		// background
		int backgroundId;
		RelativeLayout base;
		// background
		setStatusBarVisibilityForAndroidVersion(context);
		backgroundId = Integer.parseInt(prefs.getString("BackgroundPref", "0"));
		BackgroundAdapter backgroundAdapter = new BackgroundAdapter(context);
		base = (RelativeLayout) context.findViewById(R.id.base);

		Drawable background = context.getResources().getDrawable((Integer) backgroundAdapter.getItem(backgroundId));
		base.setBackgroundDrawable(background);
		return background;
	}

	
	@TargetApi(11)
	public static void setStatusBarVisibilityForAndroidVersion(Activity context) {
		RelativeLayout statusbar = (RelativeLayout) context.findViewById(R.id.statusbar);
		if (statusbar != null) {
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
				ActionBar actionBar = context.getActionBar();
				actionBar.setDisplayHomeAsUpEnabled(true);
				statusbar.setVisibility(View.GONE);
			}
		}
	}
}
